package com.sgb.knowledgeshare.weight;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.airbnb.lottie.LottieAnimationView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.knowledgeshare.R;
import com.swgk.core.base.model.entity.VideoBean;
import com.sgb.knowledgeshare.utils.OnVideoControllerListener;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import java.text.DecimalFormat;
/**
 * description:视频操作按钮
 */
public class ControllerView extends RelativeLayout implements View.OnClickListener {
    private OnVideoControllerListener listener;
    private VideoBean videoData;
    private IconFontTextView ivlikes;
    private TextView tvnickname;
    private TextView tvcontent;
    private ImageView ivhead;
    private ImageView ivfocus;
    private LottieAnimationView animationView;
    private TextView tvlikecount;
    private IconFontTextView ivshare;
    private TextView tvsharecount;
    private TextView textLabel;
    private TextView textComment;
    private RelativeLayout rllike;
    private TextView whetherFocus, commentNumber;
    private LinearLayout llComment;

    public ControllerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        View rootView = LayoutInflater.from(getContext()).inflate(R.layout.view_controller, this);
        tvnickname = rootView.findViewById(R.id.tv_nickname);//标题
        tvcontent = rootView.findViewById(R.id.tv_content);//内容
        ivhead = rootView.findViewById(R.id.iv_head);//头像
        ivfocus = rootView.findViewById(R.id.iv_focus);//关注
        ivlikes = rootView.findViewById(R.id.iv_like);//心
        animationView = rootView.findViewById(R.id.lottie_anim);//点赞整体布局
        tvlikecount = rootView.findViewById(R.id.tv_likecount);//点赞数
        ivshare = rootView.findViewById(R.id.iv_share);//分享
        tvsharecount = rootView.findViewById(R.id.tv_sharecount);//分享数
        rllike = rootView.findViewById(R.id.rl_like);//点赞
        whetherFocus = rootView.findViewById(R.id.whether_focus);//是否是你的关注
        llComment = rootView.findViewById(R.id.ll_comment);//评论布局
        commentNumber = rootView.findViewById(R.id.comment_number);//评论数
        textLabel = rootView.findViewById(R.id.text_label);//标签
        textComment = rootView.findViewById(R.id.text_comment);//评论
        rllike.setOnClickListener(this);
        ivshare.setOnClickListener(this);
        ivhead.setOnClickListener(this);
        ivfocus.setOnClickListener(this);
        llComment.setOnClickListener(this);
        textLabel.setOnClickListener(this);
        textComment.setOnClickListener(this);
    }

    //回调接口
    public void setListener(OnVideoControllerListener listener) {
        this.listener = listener;
    }

    //当前视频所有的数据信息
    public void setVideoData(VideoBean videoData, Context context,boolean isShowComment) {
        this.videoData = videoData;
        DecimalFormat df = new DecimalFormat("##0.00");
        //头像
        if (!TextUtils.isEmpty(videoData.getHeadImg())) {
            Glide.with(context).load(videoData.getHeadImg() + "")
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()).placeholder(R.mipmap.share_def_photo))
                    .into(ivhead);
        } else {
            Glide.with(context).load("")
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()).placeholder(R.mipmap.share_def_photo))
                    .into(ivhead);
        }
        int isOwner = videoData.getIsOwner();
        if (isOwner == 1) {//等于1说明属于时自己的作品，关注的加号不显示，你的关注白底不显示
            ivfocus.setVisibility(GONE);
            whetherFocus.setVisibility(GONE);
        } else {//等于0说明不属于时自己的作品
            //关注状态
            if (videoData.getIsFollow() == 1) {//已关注状态、加号不显示，你的关注白底显示
                ivfocus.setVisibility(GONE);
                whetherFocus.setVisibility(VISIBLE);
            } else if (videoData.getIsFollow() == 0) {//未关注状态、加号显示，你的关注白底不显示
                ivfocus.setVisibility(VISIBLE);
                whetherFocus.setVisibility(GONE);
            } else if (videoData.getIsFollow() == 3) {//互相关注状态、加号不显示，你的关注白底显示
                ivfocus.setVisibility(GONE);
                whetherFocus.setVisibility(VISIBLE);
            }
        }
        //   用户名称
        if (!TextUtils.isEmpty(videoData.getCreateUserName())) {
            tvnickname.setVisibility(VISIBLE);
            tvnickname.setText(videoData.getCreateUserName() + "");
        } else {
            tvnickname.setVisibility(GONE);
        }

//        标题
        if (!TextUtils.isEmpty(videoData.getTitle())) {
            tvcontent.setVisibility(VISIBLE);
            tvcontent.setText(videoData.getTitle() + "");
        } else {
            tvcontent.setVisibility(GONE);
        }
        //评论数量
        if (videoData.getCommentCount() > 9999) {
            double commentnum = videoData.getCommentCount() / 10000;
            commentNumber.setText(commentnum + "w");
        } else {
            commentNumber.setText(videoData.getCommentCount() + "");
        }
//        点赞数量
        if (!TextUtils.isEmpty(videoData.getLaudCount() + "")) {
            if (videoData.getLaudCount() > 9999) {
                double laudnum = videoData.getLaudCount() / 10000;
                tvlikecount.setText(df.format(laudnum) + "w");
            } else {
                tvlikecount.setText(videoData.getLaudCount() + "");
            }
        } else {
            tvlikecount.setText(0 + "");
        }
//        分享数量
        if (!TextUtils.isEmpty(videoData.getShareCount() + "")) {
            if (videoData.getShareCount() > 9999) {
                double sharenum = videoData.getShareCount() / 10000;
                tvsharecount.setText(df.format(sharenum) + "w");
            } else {
                tvsharecount.setText(videoData.getShareCount() + "");
            }
        } else {
            tvsharecount.setText(0 + "");
        }
       // animationView.setAnimation("like.json");
        //点赞状态
        if (videoData.isLiked() == true) {
            ivlikes.setTextColor(getResources().getColor(R.color.color_FF0041));
        } else {
            ivlikes.setTextColor(getResources().getColor(R.color.white));
        }
        //是否显示标签
        if (!TextUtils.isEmpty(videoData.getLabelName())){
            textLabel.setVisibility(VISIBLE);
            textLabel.setText("#"+videoData.getLabelName()+"");
        }else {
            textLabel.setVisibility(GONE);
        }
        //是否显示评论布局
        if (isShowComment){
            textComment.setVisibility(VISIBLE);
        }else {
            textComment.setVisibility(GONE);
        }
    }


    @Override
    public void onClick(View v) {
        if (listener == null) {
            return;
        }
        int id = v.getId();
        if (id == R.id.iv_head) {
            listener.onHeadClick(videoData);
        } else if (id == R.id.iv_share) {
            boolean isshare = permissionDiagnose(1);
            if (isshare) {
                listener.onShareClick(videoData);
                like();
            }
        } else if (id == R.id.rl_like) {
            boolean islikes = permissionDiagnose(1);
            if (islikes) {
                listener.onLikeClick(videoData);
                like();
            }
        } else if (id == R.id.iv_focus) {
            boolean focus = permissionDiagnose(2);
            if (focus) {
                listener.onFocusClick(videoData);
            }
        } else if (id == R.id.ll_comment) {
            boolean comment = permissionDiagnose(1);
            if (comment) {
                listener.onCommentClick(videoData);
            }
        } else if (id == R.id.text_comment) {
            if (!TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                if (SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH, false)) {
                    listener.onCommentSend(videoData);
                } else {
                    MToast.showToast(getContext(), "请实名认证完成后再进行评论！");
                }
            } else {
                MToast.showToast(getContext(), "请先登录账户！");
            }
        } else if (id == R.id.text_label) {
            listener.onLabelClickListener(videoData);
        }
    }

    //用户操作界面的权限判断
    private boolean permissionDiagnose(int num) {
        if (!TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
            if (SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH, false)) {
                if (num == 2) {
                    if (!SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, "").equals(videoData.getCreateUser())) {
                        return true;
                    } else {
                        MToast.showToast(getContext(), "自己不能关注自己");
                    }
                }
                return true;
            } else {
                MToast.showToast(getContext(), "请实名认证完成后再进行点赞！");
            }
        } else {
            MToast.showToast(getContext(), "请先登录账户！");
        }
        return false;
    }

    /**
     * 点赞动作
     */
    public void like() {
        if (videoData.isLiked()) {
            //点赞
            animationView.setVisibility(VISIBLE);
           // animationView.playAnimation();
            ivlikes.setTextColor(getResources().getColor(R.color.color_FF0041));
        } else {
            //取消点赞
            animationView.setVisibility(INVISIBLE);
            ivlikes.setTextColor(getResources().getColor(R.color.white));
        }
    }

}
